package com.iflytek.springboot_medicine.controller;

import com.iflytek.springboot_medicine.entity.Category;
import com.iflytek.springboot_medicine.entity.Medicine;
import com.iflytek.springboot_medicine.pojo.MedicinePojo;
import com.iflytek.springboot_medicine.pojo.ResponseMessage;
import com.iflytek.springboot_medicine.service.impl.MedicineServiceImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/medicine")
public class MedicineController {
    private final MedicineServiceImpl medicineService;
    public MedicineController(MedicineServiceImpl medicineService) {
        this.medicineService = medicineService;

    }

    /**
     * 分页查询
     * @param name
     * @param category
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findAllMedicine")
    public ResponseMessage<Page<MedicinePojo>> getMedicinesByCriteria(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String category,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        Pageable pageable = medicineService.findCategory(pageNum, pageSize);
        Page<MedicinePojo> medicines = medicineService.getMedicinesByCriteriaAsDTO(name, category, pageable);
        return ResponseMessage.success(medicines);
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @RequestMapping("/delMedicineById/{id}")
    public ResponseMessage deleteMedicine(@PathVariable int id) {
        if (id > 0){
            medicineService.deleteMedicineById(id);
        }
        return ResponseMessage.success();

    }

    /**
     * 批量删除
     */
    @RequestMapping("/deleteBatchType")
    public ResponseMessage getUserById(@RequestBody List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return ResponseMessage.error("删除失败");
        }
        medicineService.deleteAllInBatch(ids);
        return ResponseMessage.success();
    }

    /**
     * 更新药材信息
     * @param medicine
     * @return
     */
    @RequestMapping("/updateMedicine")
    public ResponseMessage<MedicinePojo> updateMedicine(@RequestBody MedicinePojo medicine) {
        if (medicine == null) {
            return ResponseMessage.error("更新失败");
        }
        String category = medicine.getCategory();
        int i = medicineService.selectByName(category);
        Medicine medicine1 = new Medicine(i,medicine.getCost_price(),medicine.getDescription(),medicine.getId(),medicine.getName(),medicine.getProduction(),medicine.getSale_price(),medicine.getStandard(),medicine.getStatus(),medicine.getStore_count());
        medicineService.saveMedicine(medicine1);
        return ResponseMessage.success(medicine);
    }

    /**
     * 增加药材信息
     * @param medicine
     * @return
     */
    @RequestMapping("/addMedicine")
    public ResponseMessage<MedicinePojo> addMedicine(@RequestBody MedicinePojo medicine) {
        if (medicine == null) {
            return ResponseMessage.error("添加失败");
        }
        String category = medicine.getCategory();
        int i = medicineService.selectByName(category);
        Medicine medicine1 = new Medicine(i,medicine.getCost_price(),medicine.getDescription(),medicine.getId(),medicine.getName(),medicine.getProduction(),medicine.getSale_price(),medicine.getStandard(),medicine.getStatus(),medicine.getStore_count());
        medicineService.addMedicine(medicine1);
        return ResponseMessage.success(medicine);
    }
}
